package cn.feng.thinkInJava.a8_1_IO.io.a18_10_04;

//: io/UsingBuffers.java
import java.nio.*;
import static net.mindview.util.Print.*;

/**
 * 用缓冲器操作数据
 * 
 * @author fengyu
 * @date 2015年8月9日
 */
public class UsingBuffers
{
	private static void symmetricScramble(CharBuffer buffer)
	{
		while(buffer.hasRemaining())
		{
			buffer.mark();
			char c1=buffer.get();
			char c2=buffer.get();
			buffer.reset();
			buffer.put(c2).put(c1);
		}
	}
	
	public static void main(String[] args)
	{
		char[] data="UsingBuffers".toCharArray();
		ByteBuffer bb=ByteBuffer.allocate(data.length*2);
		CharBuffer cb=bb.asCharBuffer();
		cb.put(data);
		print(cb.rewind());
		symmetricScramble(cb);
		print(cb.rewind());
		symmetricScramble(cb);
		print(cb.rewind());
	}
} /*
 * Output: UsingBuffers sUniBgfuefsr UsingBuffers
 */// :~
